#!/usr/bin/bash

# Copyright (c) 2022 Huawei Technologies Co.,Ltd.ALL rights reserved.
# This program is licensed under Mulan PSL v2.
# You can use it according to the terms and conditions of the Mulan PSL v2.
#          http://license.coscl.org.cn/MulanPSL2
# THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
####################################
#@Author    	:   @meitingli
#@Contact   	:   244349477@qq.com
#@Date      	:   2020-11-30
#@License   	:   Mulan PSL v2
#@Desc      	:   Take the test copy file on fs
#####################################

source ../common_lib/fsio_lib.sh

function pre_test() {
    LOG_INFO "Start environment preparation."
    point_list=($(CREATE_FS))
    LOG_INFO "End of environmental preparation!"
}

function run_test() {
    LOG_INFO "Start to run test."
    for i in $(seq 1 $((${#point_list[@]} - 1))); do
        var=${point_list[$i]}
        echo "test" >$var/testfile1
        inode1=$(stat $var/testfile1 | grep Inode | cut -d : -f 3 | awk '{print $1}')
        cp $var/testfile1 $var/testfile2
        inode2=$(stat $var/testfile2 | grep Inode | cut -d : -f 3 | awk '{print $1}')
        [[ $inode1 -ne $inode2 ]]
        CHECK_RESULT $? 0 0 "Check inode failed."
        chmod 777 $var/testfile2
        cp -p $var/testfile2 $var/testfile3
        per01=$(ls -l $var/ | grep "testfile3" | awk '{print $1}')
        [[ "$per01" =~ "-rwxrwxrwx." ]]
        mkdir -p $var/testdir1/testdir2
        cp $var/testfile2 $var/testdir1/testdir2
        cp -r $var/testdir1 $var/testdir3
        ls $var/testdir3/testdir2/testfile2
        CHECK_RESULT $? 0 0 "Check $var/testdir1/$var/testdir1/testfile2 file failed."
        echo "test1111" > $var/testfile2
        cp -f $var/testfile2 $var/testdir3/testdir2/testfile2
        grep -q "test1111" $var/testdir3/testdir2/testfile2
        CHECK_RESULT $? 0 0 "Check file failed."
    done
    LOG_INFO "End to run test."
}

function post_test() {
    LOG_INFO "Start to restore the test environment."
    list=$(echo ${point_list[@]})
    REMOVE_FS "$list"
    LOG_INFO "End to restore the test environment."
}

main $@
